package com.tools.app.ui.activity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.format.Formatter;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.ads.AdView;
import com.hm.app.R;
import com.hm.base.android.mob.AMApplication;
import com.hm.base.android.ui.util.ScreenUtil;
import com.tools.ad.ADConstant;
import com.tools.ad.EntranceType;
import com.tools.ad.bean.AppInfo;
import com.tools.app.AppApplication;
import com.tools.cleanmaster.manager.TrashManager;
import com.tools.cleanmaster.model.item.AppTrashItem;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * desc: 安装 或 卸载 广告页面
 * date: 2018/3/22
 * author: ancun
 */

public class SmartMonitorActivity extends SingleActivity implements View.OnClickListener {

    private ViewGroup mAdContainerFather;
    private ViewGroup mAdContainer;
    private View mAdView;

    private ImageView mClose;
    private ImageView mInstallAppIcon;

    private TextView mInstallAppName;
    private TextView mInstallTitle;
    private TextView mInstallDesc;
    private TextView mInstallSize;

    private String mEntranceType;

    protected AppApplication imContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imContext = (AppApplication) AMApplication.getInstance();
        handleExtra();
        if (EntranceType.APP_INSTALL.getName().equals(mEntranceType)) {
            setContentView(R.layout.activity_smart_monitor);
        } else if (EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            setContentView(R.layout.activity_smart_monitor_uninstall);
        }
        overridePendingTransition(R.anim.fade_in, 0);
        init();
    }

    public void handleExtra() {
        if (getIntent() != null) {
            int adType = getIntent().getIntExtra(ADConstant.AD_TYPE, 0);
            mEntranceType = getIntent().getStringExtra(ADConstant.ENTRANCE_TYPE);
        }
    }

    private void init() {
        initView();
        initAdView();
        initData();
        initListener();
        initHeatingLevel();
    }

    private void initData() {
        if (!EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            return;
        }
        final AppInfo appInfo = imContext.getSmartMonitorCache().getAppInfo();
        new Thread(new Runnable() {

            @Override
            public void run() {
                if (appInfo == null) {
                    return;
                }

                final List<AppTrashItem> trashList = TrashManager.getInstance().scanUninstalledAppTrash(appInfo.getPackageName());
                if (trashList != null && !trashList.isEmpty()) {
                    String appName = trashList.get(0).appName;

                    final int size = trashList.size();
                    ArrayList<String> pathList = new ArrayList<String>(size + 1);
                    for (int i = 0; i < size; i++) {
                        AppTrashItem item = trashList.get(i);
                        pathList.add(item.filePath);
                    }

                    for (String path : pathList) {
                        FileUtils.delete(new File(path));
                    }

                }
            }

        }).start();
    }

    private void initView() {
        // smart install
        mAdContainer = (ViewGroup) findViewById(R.id.ad_container);
        mAdContainerFather = (ViewGroup) findViewById(R.id.ad_container_father);

        mClose = (ImageView) findViewById(R.id.close);
        mInstallAppIcon = (ImageView) findViewById(R.id.install_app_name_icon);
        mInstallAppName = (TextView) findViewById(R.id.install_app_name);
        mInstallTitle = (TextView) findViewById(R.id.app_install_title);
        mInstallDesc = (TextView) findViewById(R.id.app_install_desc);
        mInstallSize = (TextView) findViewById(R.id.app_install_size);

    }

    private void initAdView() {
        if (EntranceType.APP_INSTALL.getName().equals(mEntranceType)) {
            mAdView = imContext.getADModule().getAdSceneManager().getAdView(EntranceType.APP_INSTALL);
        } else if (EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            mAdView = imContext.getADModule().getAdSceneManager().getAdView(EntranceType.APP_UNINSTALL);
        }
        //卸载场景 , 如果是admob banner 重设一下padding
        if (mAdView instanceof AdView && EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            mAdContainerFather.setPadding(0, ScreenUtil.dp2px(this, 9), 0, ScreenUtil.dp2px(this, 9));
        }

        if (mAdView != null) {
            mAdContainer.removeAllViews();
            mAdContainer.addView(mAdView);
        }
    }

    private void initListener() {
        mClose.setOnClickListener(this);
    }

    private void initHeatingLevel() {
        if (!EntranceType.APP_INSTALL.getName().equals(mEntranceType)) {
            return;
        }

        AppInfo appInfo = imContext.getSmartMonitorCache().getAppInfo();
        if (appInfo == null) {
            return;
        }
        mInstallAppIcon.setImageDrawable(appInfo.getIcon());
        mInstallAppName.setText(appInfo.getLabel());

        String heatingLevelDesc = "";
        String heatingLevel = "";
        int heatingLevelColor = 0;
        Drawable face = new BitmapDrawable(getResources());

        if (appInfo.getSize() >= 20 * 1024 * 1024 || appInfo.getSize() == 0) {
            heatingLevelDesc = getResources().getString(R.string.smart_monitor_high_level_desc);
            heatingLevel = getResources().getString(R.string.smart_monitor_high_level);
            face = getResources().getDrawable(R.drawable.smart_monitor_high);
            heatingLevelColor = getResources().getColor(R.color.smart_monitor_high_level);

        } else if (appInfo.getSize() < 20 * 1024 * 1024 && appInfo.getSize() >= 8 * 1024 * 1024) {
            heatingLevelDesc = getResources().getString(R.string.smart_monitor_medium_level_desc);
            heatingLevel = getResources().getString(R.string.smart_monitor_medium_level);
            face = getResources().getDrawable(R.drawable.smart_monitor_medium);
            heatingLevelColor = getResources().getColor(R.color.smart_monitor_medium_level);

        } else if (appInfo.getSize() < 8 * 1024 * 1024) {
            heatingLevelDesc = getResources().getString(R.string.smart_monitor_low_level_desc);
            heatingLevel = getResources().getString(R.string.smart_monitor_low_level);
            face = getResources().getDrawable(R.drawable.smart_monitor_low);
            heatingLevelColor = getResources().getColor(R.color.smart_monitor_low_level);

        }

        if (appInfo.getSize() > 0) {
            mInstallSize.setVisibility(View.VISIBLE);
            mInstallSize.setText(getString(R.string.smart_monitor_install_size, Formatter.formatFileSize(this, appInfo.getSize())));
        } else {
            mInstallSize.setVisibility(View.GONE);
        }

        mInstallDesc.setText(heatingLevelDesc);

        Spannable span = new SpannableString(heatingLevel);
        face.setBounds(0, 0, ScreenUtil.dp2px(imContext, 18), ScreenUtil.dp2px(imContext, 18));
        ImageSpan prefixSpan = new ImageSpan(face, ImageSpan.ALIGN_BASELINE);
        span.setSpan(prefixSpan, heatingLevel.length() - 7, heatingLevel.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        mInstallTitle.setText(span);
        mInstallTitle.setTextColor(heatingLevelColor);
    }

    @Override
    public void subHandleMessage(Message msg) {
        super.subHandleMessage(msg);

        switch (msg.what) {

            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdContainer.removeAllViews();
        mAdView = null;

        if (EntranceType.APP_INSTALL.getName().equals(mEntranceType)) {
            imContext.getADModule().getAdSceneManager().release(EntranceType.APP_INSTALL);
        } else if (EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            imContext.getADModule().getAdSceneManager().release(EntranceType.APP_UNINSTALL);
        }

        if (EntranceType.APP_INSTALL.getName().equals(mEntranceType)) {
            imContext.getADModule().getAdSceneManager().load(EntranceType.APP_INSTALL_DEFER);
        } else if (EntranceType.APP_UNINSTALL.getName().equals(mEntranceType)) {
            imContext.getADModule().getAdSceneManager().load(EntranceType.APP_UNINSTALL_DEFER);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.fade_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                if (!isFinishing()) {
                    finish();
                }
                break;

            default:
                break;
        }
    }


}
